package cn.jhz.learn.blog.service.security.resource.article.impl;

import cn.jhz.learn.blog.common.exception.ParamException;
import cn.jhz.learn.blog.service.security.resource.article.SubfieldValidationService;
import cn.jhz.learn.blog.vo.SubfieldPageView;
import cn.jhz.learn.blog.vo.article.query.page.ConsolePageView;
import cn.jhz.learn.blog.vo.article.submit.SubfieldParam;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SubfieldValidationAspect extends SubfieldPointCutImpl{

    private final SubfieldValidationService validationService;

    @Autowired
    public SubfieldValidationAspect(SubfieldValidationService validationService) {
        this.validationService = validationService;
    }

    @Before(value = "create(uid,param)", argNames = "uid,param")
    public void createParamVerification(Integer uid, @NotNull SubfieldParam param){
        validationService.blogValid(uid);
        if(param.getImgUrl().isPresent()){
            if(StringUtils.isNotBlank(param.getImgUrl().get()));
            /*TODO:待设计文件系统*/
//                        validationService.fileValid(uid, subfield.getImgUrl().get());
        }
    }

    @Before(value = "delete(uid,id,deep)", argNames = "uid,id,deep")
    public void deleteParamVerification(Integer uid, Integer id, Boolean deep) {
        validationService.validStrict(uid, id);
    }

    @Before(value = "update(uid,param)", argNames = "uid,param")
    public void updateParamVerification(Integer uid, @NotNull SubfieldParam param) {
        if(param.getId().isPresent())
            validationService.validStrict(uid, param.getId().get());
        else
            throw new ParamException("缺少分支id");
    }

    @Before(value = "stick(uid,id,sticky)", argNames = "uid,id,sticky")
    public void stickParamVerification(Integer uid, Integer id, Boolean sticky) {
        validationService.validStrict(uid, id);
}

    @Before(value = "recycle(uid,id)", argNames = "uid,id")
    public void recycleParamVerification(Integer uid, Integer id) {
        validationService.validStrict(uid, id);
    }

    @Before(value = "getDetail(id)", argNames = "id")
    public void getDetailParamVerification(Integer id) {
        validationService.valid(id);
    }

    @Before(value = "getDetailUid(uid,id)", argNames = "uid,id")
    public void getDetailUidParamVerification(Integer uid, Integer id) {
        validationService.valid(uid, id);
    }

    @Before(value = "getDetailConsole(uid,id)", argNames = "uid,id")
    public void getConsoleDetailParamVerification(Integer uid, Integer id){
        validationService.valid(uid, id);
    }

    @Before(value = "getPage(pageView)", argNames = "pageView")
    public void getPageParamVerification(@NotNull SubfieldPageView pageView) {
        if(pageView.getBlog_name().isPresent())
            validationService.blogValid(pageView.getBlog_name().get());
        else
            throw new ParamException("缺少查询条件！");
    }

    @Before(value = "getPageUid(uid,pageView)", argNames = "uid,pageView")
    public void getPageUidParamVerification(Integer uid, @NotNull SubfieldPageView pageView) {
        if(pageView.getBlog_name().isPresent())
            validationService.blogValid(uid, pageView.getBlog_name().get());
        else
            validationService.blogValid(uid);
    }

    @Before(value = "getCountAndList(uid, pageView)", argNames = "uid,pageView")
    public void getCountAndListParamVerification(Integer uid, @NotNull ConsolePageView pageView){
        if(!pageView.getStatus().isPresent())
            throw new ParamException("缺少参数, 没有查询条件！");
        validationService.blogValid(uid);
    }
}
